<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="charging"></div>
    <div id="electricQuantity"></div>
    <div id="batteryLife"></div>
    <div id="chargeTime"></div>
    <pre>
        <code>
      navigator.getBattery().then((battery) => {
        function updateAllBatteryInfo() {
          updateChargeInfo();
          updateLevelInfo();
          updateChargingInfo();
          updateDischargingInfo();
        }
        updateAllBatteryInfo();

        function updateChargeInfo() {
          $('#charging').text(
            `电池是否充电中？${battery.charging ? '是' : '否'}`
          );
        }
        function updateLevelInfo() {
          $('#electricQuantity').text(`电池电量：${battery.level * 100}%`);
        }
        function updateChargingInfo() {
          $('#batteryLife').text(`电池续航时间：${battery.dischargingTime}秒`);
        }
        function updateDischargingInfo() {
          $('#chargeTime').text(`电池充电时间：${battery.chargingTime}秒`);
        }
        battery.addEventListener('chargingchange', () => {
          updateChargeInfo();
        });
        battery.addEventListener('levelchange', () => {
          updateLevelInfo();
        });
        battery.addEventListener('chargingtimechange', () => {
          updateChargingInfo();
        });
        battery.addEventListener('dischargingtimechange', () => {
          updateDischargingInfo();
        });
      });
        </code>
    </pre>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
      navigator.getBattery().then((battery) => {
        function updateAllBatteryInfo() {
          updateChargeInfo();
          updateLevelInfo();
          updateChargingInfo();
          updateDischargingInfo();
        }
        updateAllBatteryInfo();

        function updateChargeInfo() {
          $('#charging').text(
            `电池是否充电中？${battery.charging ? '是' : '否'}`
          );
        }
        function updateLevelInfo() {
          $('#electricQuantity').text(`电池电量：${battery.level * 100}%`);
        }
        function updateChargingInfo() {
          $('#batteryLife').text(`电池续航时间：${battery.dischargingTime}秒`);
        }
        function updateDischargingInfo() {
          $('#chargeTime').text(`电池充电时间：${battery.chargingTime}秒`);
        }
        battery.addEventListener('chargingchange', () => {
          updateChargeInfo();
        });
        battery.addEventListener('levelchange', () => {
          updateLevelInfo();
        });
        battery.addEventListener('chargingtimechange', () => {
          updateChargingInfo();
        });
        battery.addEventListener('dischargingtimechange', () => {
          updateDischargingInfo();
        });
      });
    </script>
  </body>
</html>
